<script setup lang="ts">
import useDashboardStore from '@/store/dashboard/dashboard'
import * as echarts from 'echarts'
import { storeToRefs } from 'pinia'
import { onMounted, ref, watchEffect } from 'vue'

const pieRef = ref<HTMLElement>()
const dashboardStore = useDashboardStore()
const { categoryCountDate } = storeToRefs(dashboardStore)

onMounted(() => {
  const myChart = echarts.init(pieRef.value!)

  watchEffect(() =>
    myChart.setOption({
      title: {
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        left: '5%'
      },
      series: [
        {
          center: ['50%', '60%'],
          name: '访问来源',
          type: 'pie',
          radius: '60%',
          data: categoryCountDate.value,
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    })
  )

  window.addEventListener('resize', () => {
    myChart.resize()
  })
})
</script>

<template>
  <div class="piechart">
    <div class="header">
      <h2 class="title">分类商品数量(玫瑰图)</h2>
    </div>
    <div class="content" ref="pieRef"></div>
  </div>
</template>

<style lang="less" scoped>
.piechart {
  height: 453px;
  background-color: #fff;
  border-radius: 4px;
}
.header {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  height: 50px;
  padding: 10px 20px;
  border-bottom: 1px solid #e4e7ed;
  box-sizing: border-box;
  .title {
    font-size: 18px;
    font-weight: 400;
  }
}
.content {
  padding: 20px;
  height: 350px;
}
</style>
